// pages/course/voiceCourse/my/my.js
const common = require('../../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        navType: 1,
        courseList: [],
        nowPage: 0,
        isLoading: false,
        isAll: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getCourseList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getCourseList()
    },

    selectNav: function(e) {
        if (this.data.navType == e.currentTarget.dataset.type) {
            return
        }
        if (this.data.isLoading) {
            return
        }
        this.setData({
            navType: e.currentTarget.dataset.type,
            courseList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        this.getCourseList()
    },

    getCourseList: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        const that = this
        let courseList = this.data.courseList
        if (this.data.navType == 1) {
            let data = {
                type: 1,
                page: this.data.nowPage,
                limit: 10
            }
            common.post('Online/get_online_collect', data).then(function(res) {
                that.setData({
                    isLoading: false
                })
                wx.hideLoading()
                if (res.code == 204) {
                    that.setData({
                        courseList: courseList,
                        isAll: true
                    })
                } else {
                    courseList = courseList.concat(res)
                    that.setData({
                        courseList: courseList,
                        nowPage: that.data.nowPage + 1
                    })
                }
            }).catch(function(e) {})
        } else {
            let data = {
                is_pay: 1,
                type: 1,
                page: this.data.nowPage,
                limit: 10
            }
            common.post('Online/get_online_buy_list', data).then(function(res) {
                that.setData({
                    isLoading: false
                })
                wx.hideLoading()
                if (res.code == 204) {
                    that.setData({
                        courseList: courseList,
                        isAll: true
                    })
                } else {
                    courseList = courseList.concat(res)
                    that.setData({
                        courseList: courseList,
                        nowPage: that.data.nowPage + 1
                    })
                }
            }).catch(function(e) {})
        }
    },

    goDetail: function(e) {
        wx.navigateTo({
            url: '/pages/course/voiceCourse/detail/detail?courseId=' + e.currentTarget.dataset.id
        })
    },

    cancleCollect: function(e) {
        const that = this
        let data = {
            online_id: e.currentTarget.dataset.id
        }
        common.post('Online/del_online_collect', data).then(function(res) {
            if (res.code == 204) {
                wx.showToast({
                    title: res.error,
                    icon: 'none',
                    duration: 1000
                })
            } else {
                wx.showModal({
                    title: '提示',
                    content: '取消收藏成功',
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            that.setData({
                                navType: 1,
                                courseList: [],
                                nowPage: 0,
                                isLoading: false,
                                isAll: false
                            })
                            that.getCourseList()
                        }
                    }
                })
            }
        }).catch(function(e) {})
    }
})